/* javascript for views_bug/edit.html */

function init_attachment_upload() {
    $("#bug-attachment-upload").fileupload({
        dataType: "json",
        add: function(e, data) {
            // 判断附件名称是否存在
            for (var i = 0; i < $("#tb-attachments > tbody > tr").length; i++) {
                var cur_filename = $("#tb-attachments > tbody > tr:eq(" + i + ") > td:eq(1)").text();
                if (data.files[0].name == cur_filename) {
                    alert("此附件名称已经存在!");
                    return false;
                }
            }
            data.submit();
        },
        done: function(e, data) {
            var jsondata = data.result;
            if (jsondata.result == "NG") {
                alert(jsondata.message);
                return;
            }

            var order = $("#tb-attachments > tbody > tr").length + 1;
            var tr_html = '<tr>';
            tr_html += '<td>' + order + '</td>';
            tr_html += '<td>' + jsondata.ba_name + '</td>';
            tr_html += '<td><button type="button" class="btn btn-link" onclick="download_attachment(' + jsondata.ba_id + ');"><i class="fa fa-download fa-fw"></i> 下载</button></td>';
            tr_html += '<td><button type="button" class="btn btn-danger btn-xs" onclick="del_attachment(this);"><i class="fa fa-trash-o fa-fw"></i> 删除</button></td>';
            tr_html += '<td class="hidden">' + jsondata.ba_id + '</td>';
            tr_html += '</tr>';

            $("#tb-attachments > tbody").append(tr_html);
        },
    });

}

/* 下载已经上传的附件 */
function download_attachment(ba_id) {
    location.href = "/bug/download-attachment/" + ba_id;
}

function review_attachment(path) {
    alert(path);
    $("#review_image").attr("src", path);
}

/* 删除已经上传的附件 */
function del_attachment(btn) {
    $(btn).parent().parent().remove();
    // 重新设置附件的order, 也就是 # 下的内容
    for (var i = 0; i < $("#tb-attachments > tbody >tr").length; i++) {
        $("#tb-attachments > tbody >tr:eq(" + i + ") td:eq(0)").text(i + 1);
    }
}

/* 保存新建或者修改的bug */
function save_bug() {
    var req = new Chujue_Request();
    var proj_id = $("#sel-project").val();
    var bug_id = $("#hid-bug-id").val();
    var bug_title = $("#txt-bug-title").val();
    var bug_desc = $("#txt-bug-desc").val();
    var bi_code = $("#sel-importance").val();
    var bs_code = $("#sel-state").val();
    var assign_to_id = $("#sel-assign-to").val();
    var attachment_ids = new Array();
    for (var i = 0; i < $("#tb-attachments > tbody >tr").length; i++) {
        var attachment_id = $("#tb-attachments > tbody >tr:eq(" + i + ") td:eq(0)").text();
        attachment_ids.push(attachment_id);
    }

    req.url = "/bug/save";
    req.is_setmessage = false;
    req.send_data = {
        "proj_id": proj_id,
        "bug_id": bug_id,
        "bug_title": bug_title,
        "bug_desc": bug_desc,
        "bi_code": bi_code,
        "bs_code": bs_code,
        "assign_to_id": assign_to_id,
        "attachment_ids": attachment_ids,
        "comment": $("#txt-bug-comment").val(),
    };
    req.ok_func = function(data) {
        location.href = "/bug/list";
    };
    req.ajax_req();
}

/* 删除bug, 目前只有创建bug的人才能删除 */
function del_bug() {
    if (!confirm("确定删除此bug?"))
        return;

    var req = new Chujue_Request();
    var bug_id = $("#hid-bug-id").val();

    req.url = "/bug/delete";
    req.send_data = {
        "bug_id": bug_id,
    };
    req.ok_func = function(data) {
        location.href = "/bug/list";
    };
    req.ajax_req();
}
